package com.rasenhund.android.grecorder.activity;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.adapter.HistoryListAdapter;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;
import com.rasenhund.android.grecorder.schema.StageRecord;

/**
 * 最近の出撃画面
 */
public class HistoryListActivity extends BaseListActivity {
    @SuppressWarnings("unused")
    private static final String TAG = HistoryListActivity.class.getSimpleName();
    private final HistoryListActivity self = this;

    HistoryListAdapter mAdapter = null;

    // TODO:後でダイアログの出し方は見直す
    private int mRowId = -1;
    private int mPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_history);

        initView();

    }

    /**
     * アイテム長押し時のイベントリスナ
     */
    final OnItemLongClickListener lstHistory_OnItemLongClickListener = new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            // TODO: グローバル変数は良くないので直したい
            mPosition = position;
            mRowId = (int) id;

            AlertDialog.Builder builder = null;
            builder = new AlertDialog.Builder(self);
            builder.setMessage(getStr(R.string.msg_confirm_delete));
            builder.setPositiveButton(getStr(R.string.msg_yes), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (mRowId != -1) {
                        
                        deleteAt(mRowId);
                        
                    }

                    if (mPosition != -1) {

                        mAdapter.removeAt(mPosition);
                        mAdapter.notifyDataSetChanged();

                    }

                    mPosition = -1;
                    mRowId = -1;

                }
            });
            builder.setNegativeButton(getStr(R.string.msg_no), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    mPosition = -1;
                    mRowId = -1;

                    dialog.cancel();
                }
            });
            builder.show();

            // イベントはここで消化
            return true;
        }
    };

    /**
     * Viewの初期化
     */
    private void initView() {

        ArrayList<StageRecord> list = null;

        // 表示用データの取得
        // TODO: open - close をtry - catchなり必ずcloseするように
        DbHelper dbHelper = new DbHelper(self);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        RecordDao dao = new RecordDao(db);

        list = dao.selectAll(false);

        db.close();

        // viewの初期化
        mAdapter = new HistoryListAdapter(self, list);

        ListView lstHistory = getListView();
        lstHistory.setOnItemLongClickListener(lstHistory_OnItemLongClickListener);
        lstHistory.setAdapter(mAdapter);

    }

    /**
     * 一件削除
     * 
     * @param rowId
     */
    private void deleteAt(int rowId) {

        // TODO: open-closeはできれば共通化したい
        DbHelper dbHelper = new DbHelper(self);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        RecordDao dao = new RecordDao(db);

        int iRet = dao.deleteAt(rowId);

        db.close();

        if (iRet != 0) {
            String sMsg = getStr(R.string.msg_delete_complete);
            Toast.makeText(self, sMsg, Toast.LENGTH_SHORT).show();
        }

    }

}
